//
//  String+Extensions.swift
//  Xcode正则
//
//  Created by 中发 on 2018/6/21.
//  Copyright © 2018年 中发. All rights reserved.
//

import Foundation

extension String {
    
    /// 从当前字符串中，提取链接和文本
    //Swift提供了‘元组’，同事返回多个值
    //如果是OC，可以返回字典/自定义对象/指针的指针
    func tyc_href() -> (link: String, text: String)? {
        
        let pattern = "<a href=\"(.*?)\" .*?>(.*?)</a>"
        
        guard let regx = try? NSRegularExpression(pattern: pattern, options: []), let result = regx.firstMatch(in: self, options: [], range: NSRange(location: 0, length: count)) else {
            return nil
        }
        let link = (self as NSString).substring(with: result.range(at: 1))
        let text = (self as NSString).substring(with: result.range(at: 2))
//        print(link + "----" + text)
        return (link, text)
    }
}
